<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 放置内容 -->
        <el-tabs>
          <el-tab-pane label="角色管理">
            <!-- 左侧内容 -->
            <el-row style="height:60px;">
              <el-button size="small" icon="el-icon-plus" type="primary" @click="increased">新增角色</el-button>
            </el-row>
            <el-table border :data="list">
              <el-table-column header-align="center" width="120" label="序号" type="index"></el-table-column>
              <el-table-column header-align="center" width="240" label="角色名称" prop="name"></el-table-column>
              <el-table-column header-align="center" label="描述" prop="description"></el-table-column>
              <!-- 作用于插槽 结构出来的 row list数组中 该行的数据 可以通过row 拿到每一行的id 进行删除 编辑操作 -->
              <el-table-column label="操作" v-slot="{row}">
                <el-button type="success" size="small" icon="el-icon-coordinate">分配权限</el-button>
                <el-button
                  type="primary"
                  size="small"
                  icon="el-icon-edit"
                  @click="editRole(row.id)"
                >编辑</el-button>
                <!-- @click="editRole(row.id)" -->
                <el-button
                  type="danger"
                  size="small"
                  icon="el-icon-delete"
                  @click="deleteRole(row.id)"
                >删除</el-button>
              </el-table-column>
            </el-table>
            <!-- 放置分页组件 -->
            <el-row type="flex" justify="center" style="height:60px" align="middle">
              <el-pagination
                layout="prev, pager, next"
                :page-size="page.pagesize"
                :total="page.total"
                :current-page="page.page"
                @current-change="changePage"
              ></el-pagination>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 右侧内容 -->
            <el-form label-width="120px" style="margin-top:50px">
              <el-form-item label="企业名称">
                <el-input v-model="formData.name" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input v-model="formData.companyAddress" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input v-model="formData.companyPhone" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input v-model="formData.mailbox" disabled style="width:400px"></el-input>
              </el-form-item>
              <el-form-item label="备注">
                <el-input v-model="formData.remarks" disabled style="width:400px"></el-input>
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 点击新增或者编辑显示的弹窗 -->
    <tanchuang ref="shanchu" :showDialog.sync="showDialog" :zzcID="zzcID" @updateRole="getRoleList"></tanchuang>
  </div>
</template>

<script>
import { getRoleList, getCompanyInfo, deleteRole } from "@/api/setting";
import { mapGetters } from "vuex";
import tanchuang from "./components/tanchuan";
export default {
  name: "setting",
  data() {
    return {
      showDialog: false,
      list: [], // 存放新增表格中的数据
      page: {
        // 定义分页的初始值
        page: 1,
        pagesize: 10,
        total: 0
      },
      formData: {}, // 存放公司信息
      // 定义规则,
      zzcID: ""
    };
  },
  created() {
    this.getRoleList();
    this.getCompanyInfo();
  },
  props: {},
  methods: {
    // 获取角色管理中的数据
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page);
      this.page.total = total;
      this.list = rows;
      console.log(rows);
    },
    // 给 分页绑定的事件
    changePage(newPage) {
      this.page.page = newPage; // 将当前页码赋值给当前的最新页码
      this.getRoleList();
    },
    // 通过企业id 获取到公司信息
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(this.companyId);
    },
    // 删除
    async deleteRole(id) {
      try {
        await this.$confirm("确定删除该角色吗");
        await deleteRole(id);
        this.getRoleList();
        this.$message.success("角色删除成功");
      } catch (error) {
        console.log(error);
      }
    },
    async editRole(id) {
      // this.$emit("update:showDialog", true);
      await this.$refs.shanchu.editRole(id);
      this.showDialog = true;
      this.zzcID = id;
    },
    increased(){
      this.showDialog = true
    }
  },
  computed: {
    ...mapGetters(["companyId"])
  },
  components: {
    tanchuang
  }
};
</script>

<style>
</style>
